package hunt.treasure.scenes;

import hunt.treasure.GameActivity;
import hunt.treasure.config.Define;
import hunt.treasure.config.ImageInfos;

import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.util.modifier.IModifier;
import org.andengine.util.modifier.ease.EaseBounceIn;
import org.andengine.util.modifier.ease.EaseBounceInOut;
import org.andengine.util.modifier.ease.EaseCircularInOut;
import org.andengine.util.modifier.ease.EaseExponentialOut;
import org.andengine.util.modifier.ease.EaseQuartInOut;
import org.andengine.util.modifier.ease.EaseStrongInOut;

public class MenuScene extends AbstractScene {
	private ITextureRegion mBackgroundTextureRegion;
	private Sprite mBackgroundSprite;
	private BitmapTextureAtlas mBackgroundBitmapTextureAtlas;
	private ITextureRegion mLoginTextureRegion;
	private Sprite mLoginSprite;
	private ITextureRegion mRegisterTextureRegion;
	private Sprite mRegisterSprite;
	private ITextureRegion mFacebookTextureRegion;
	private Sprite mFacebookSprite;
	private BitmapTextureAtlas mMenuBitmapTextureAtlas;
	private float mScaleBg = 1.0f;

	@Override
	public void loadResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		// create background
		mBackgroundBitmapTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), ImageInfos.MENU_BG_WIDTH,
				ImageInfos.MENU_BG_HEIGHT, TextureOptions.BILINEAR);
		mBackgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(mBackgroundBitmapTextureAtlas,
						activity.getAssets(), ImageInfos.SPLASH_NAME, 0, 0);
		mBackgroundBitmapTextureAtlas.load();
		final float x = res.camera.getWidth() / 2;
		final float y = res.camera.getHeight() / 2;
		// calculate scaled ratio again
		Define.SCALED_RATIO = ((float) GameActivity.CW)
				/ (mBackgroundTextureRegion.getWidth() * Define.SCALED_ORIGINAL_RESOURCE_RATIO);

		mBackgroundSprite = new Sprite(x, y, mBackgroundTextureRegion, res.vbom);
		mScaleBg = Define.SCALED_RATIO * Define.SCALED_ORIGINAL_RESOURCE_RATIO;
		mBackgroundSprite.setScale(mScaleBg);
		// create buttons
		mMenuBitmapTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), ImageInfos.LOGIN_BUTTON_WIDTH
						+ ImageInfos.REGISTER_BUTTON_WIDTH
						+ ImageInfos.FACEBOOK_BUTTON_WIDTH,
				ImageInfos.LOGIN_BUTTON_HEIGHT, TextureOptions.BILINEAR);
		mFacebookTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(mMenuBitmapTextureAtlas, activity.getAssets(),
						ImageInfos.FACEBOOK_BUTTON_NAME, 0, 0);
		mLoginTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(mMenuBitmapTextureAtlas, activity.getAssets(),
						ImageInfos.LOGIN_BUTTON_NAME,
						ImageInfos.FACEBOOK_BUTTON_WIDTH, 0);
		mRegisterTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(mMenuBitmapTextureAtlas, activity.getAssets(),
						ImageInfos.REGISTER_BUTTON_NAME,
						ImageInfos.FACEBOOK_BUTTON_WIDTH
								+ ImageInfos.REGISTER_BUTTON_WIDTH, 0);
		mMenuBitmapTextureAtlas.load();
		// // create sprite button
		final float loginX = x;
		final float loginY = 3.0f * mLoginTextureRegion.getHeight() * mScaleBg
				/ 4.0f;
		final float dx = 3 * mScaleBg * mLoginTextureRegion.getWidth() / 2.0f;
		mLoginSprite = new Sprite(loginX, loginY, mLoginTextureRegion, res.vbom);
		mLoginSprite.setScale(mScaleBg);
		registerTouchArea(mLoginSprite);

		mFacebookSprite = new Sprite(loginX - dx, loginY,
				mFacebookTextureRegion, res.vbom);
		mFacebookSprite.setScale(mScaleBg);
		registerTouchArea(mFacebookSprite);

		mRegisterSprite = new Sprite(loginX + dx, loginY,
				mRegisterTextureRegion, res.vbom);
		mRegisterSprite.setScale(mScaleBg);
		registerTouchArea(mRegisterSprite);
	}

	@Override
	public void create() {

		final IEntityModifier.IEntityModifierListener modifierListener = new IEntityModifier.IEntityModifierListener() {

			@Override
			public void onModifierStarted(IModifier<IEntity> pModifier,
					IEntity pItem) {

			}

			@Override
			public void onModifierFinished(IModifier<IEntity> pModifier,
					IEntity pItem) {
				mBackgroundSprite.setIgnoreUpdate(true);

				final float loginY = mLoginSprite.getY();
				mLoginSprite.setY(res.camera.getHeight());
				attachChild(mLoginSprite);
				mLoginSprite
						.registerEntityModifier(new MoveModifier(1.5f,
								mLoginSprite.getX(), res.camera.getHeight(),
								mLoginSprite.getX(), loginY, EaseBounceIn
										.getInstance()));

				final float facebookX = mFacebookSprite.getX();
				mFacebookSprite.setX(0);
				attachChild(mFacebookSprite);
				mFacebookSprite.registerEntityModifier(new MoveModifier(1.5f,
						0, mFacebookSprite.getY(), facebookX, mFacebookSprite
								.getY(), EaseQuartInOut.getInstance()));

				final float registerX = mRegisterSprite.getX();
				mRegisterSprite.setX(res.camera.getWidth());
				attachChild(mRegisterSprite);
				mRegisterSprite.registerEntityModifier(new MoveModifier(1.5f,
						res.camera.getWidth(), mRegisterSprite.getY(),
						registerX, mRegisterSprite.getY(), EaseQuartInOut
								.getInstance()));
			}
		};

		final float backgroundY = mBackgroundSprite.getY();
		mBackgroundSprite.setY(res.camera.getHeight());
		attachChild(mBackgroundSprite);
		mBackgroundSprite.registerEntityModifier(new MoveModifier(2,
				mBackgroundSprite.getX(), res.camera.getHeight(),
				mBackgroundSprite.getX(), backgroundY, modifierListener,
				EaseBounceInOut.getInstance()));

	}

	@Override
	public void unloadResources() {
		// TODO Auto-generated method stub

	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onBackKeyPressed() {
		super.onBackKeyPressed();
		res.activity.finish();
	}

}
